package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.MonthlySettlementsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.MonthlySettlementsForm;
import com.youlai.boot.factory.model.query.MonthlySettlementsQuery;
import com.youlai.boot.factory.model.vo.MonthlySettlementsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 月度结算单前端控制层
 *
 * @author youlaitech
 * @since 2025-07-13 00:16
 */
@Tag(name = "月度结算单接口")
@RestController
@RequestMapping("/api/v1/monthly-settlements")
@RequiredArgsConstructor
public class MonthlySettlementsController  {

    private final MonthlySettlementsService monthlySettlementsService;

    @Operation(summary = "月度结算单分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:monthly-settlements:query')")
    public PageResult<MonthlySettlementsVO> getMonthlySettlementsPage(MonthlySettlementsQuery queryParams ) {
        IPage<MonthlySettlementsVO> result = monthlySettlementsService.getMonthlySettlementsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增月度结算单")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:monthly-settlements:add')")
    public Result<Void> saveMonthlySettlements(@RequestBody @Valid MonthlySettlementsForm formData ) {
        boolean result = monthlySettlementsService.saveMonthlySettlements(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取月度结算单表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:monthly-settlements:edit')")
    public Result<MonthlySettlementsForm> getMonthlySettlementsForm(
        @Parameter(description = "月度结算单ID") @PathVariable Long id
    ) {
        MonthlySettlementsForm formData = monthlySettlementsService.getMonthlySettlementsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改月度结算单")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:monthly-settlements:edit')")
    public Result<Void> updateMonthlySettlements(
            @Parameter(description = "月度结算单ID") @PathVariable Long id,
            @RequestBody @Validated MonthlySettlementsForm formData
    ) {
        boolean result = monthlySettlementsService.updateMonthlySettlements(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除月度结算单")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:monthly-settlements:delete')")
    public Result<Void> deleteMonthlySettlementss(
        @Parameter(description = "月度结算单ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = monthlySettlementsService.deleteMonthlySettlementss(ids);
        return Result.judge(result);
    }
}
